'use strict';
AKONG.require(['config', 'ua', 'query', 'req', 'others', 'Loading', 'AutoPlay'], function(config, ua, query, req, others, Loading, AutoPlay) {
	//svg
	var load_svg = query.className('load_svg')[0],
		edit_svg = query.className('edit_svg')[0];
	var ns = 'http://www.w3.org/2000/svg',
		xl = 'http://www.w3.org/1999/xlink',
		svg, svgs = [];

	function initSvg(name) { //加载和初始化svg
		req.ajax({
			url: name,
			dataType: 'text',
			callback: function(result) {
				load_svg.innerHTML = result;
				svg = load_svg.childNodes[0];
				SvgToCanvas(function() {
					initThree(svg.getElementById('model_name').textContent.trim(), function() {
						console.log('进入网站');
					});
				});
			}
		});
	}

	//canvas
	var canvas0 = query.className("canvas0")[0];

	function SvgToCanvas(callback) { //svg转成canvas位图然后更新材质
		canvg(canvas0, svg.outerHTML, {
			ignoreClear: true,
			ignoreMouse: true,
			renderCallback: function() {
				canvasTexture && (canvasTexture.needsUpdate = true) && render();
				callback && callback();
			}
		});
	}

	//three
	var scene, camera, renderer, controls, group, mesh, canvasTexture;
	var game = document.getElementsByClassName("game")[0],
		sceneWidth = game.offsetWidth,
		sceneHeight = game.offsetHeight,
		backgroundColor = 0x373e4e,
		animated_id = null;

	function initThree(name, callback) {
		scene = new THREE.Scene();
		group = new THREE.Group();
		scene.add(group);

		camera = new THREE.PerspectiveCamera(75, sceneWidth / sceneHeight, 1, 100);
		scene.add(camera);

		renderer = new THREE.WebGLRenderer({
			antialias: true
		});
		renderer.setClearColor(backgroundColor);
		renderer.setPixelRatio(window.devicePixelRatio);
		renderer.setSize(sceneWidth, sceneHeight);
		game.insertAdjacentElement('afterBegin', renderer.domElement);

		//light
		scene.add(new THREE.AmbientLight(0x222222));
		var light = new THREE.DirectionalLight(0xffffff);
		light.position.set(1, 1, 1).normalize();
		camera.add(light);

		//mesh
		var objLoader = new THREE.OBJLoader();
		objLoader.load(name, function(object) {
			var box, size, w, s;
			mesh = object;
			box = new THREE.Box3();
			box.setFromObject(mesh);
			size = box.getSize();
			w = Math.max(size.x, size.y, size.z);
			s = 50 / w;
			mesh.scale.set(s, s, s);
			
			box = new THREE.Box3();
			box.setFromObject(mesh);
			mesh.position.sub(box.getCenter());
			camera.position.z = box.getSize().z * 2.5;

			canvasTexture = new THREE.CanvasTexture(canvas0);
			var material = new THREE.MeshPhongMaterial({
				side: THREE.DoubleSide,
				map: canvasTexture
			});
			mesh.traverse(function(child) {
				if(child instanceof THREE.Mesh) {
					child.material = material;
				}
			});
			scene.add(mesh);

			callback && callback();
			render();
		});

		controls = new THREE.OrbitControls(camera, game);
		controls.enableZoom = false;

		controls.addEventListener('change', function() {
			render();
		});
	}

	function render() {
		renderer.render(scene, camera);
	}

	function animate() {
		render();
		animated_id = requestAnimationFrame(animate);
	}

	initSvg('models/15-MC02072/1.svg');
});